﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IndyMosaic.Models.Web
{
    public class BinaryResourceRepository
    {
        WebDataContext context = new WebDataContext();
        Dictionary<string, string> ContentTypesByExtension = new Dictionary<string, string>();

        public BinaryResourceRepository()
        {
            LoadContentTypes();
        }

        private void LoadContentTypes()
        {
            ContentTypesByExtension.Add("gif", "image/gif");
            ContentTypesByExtension.Add("png", "image/x-ping");
            ContentTypesByExtension.Add("jpeg", "image/jpeg");
            ContentTypesByExtension.Add("jpg", "image/jpeg");
            ContentTypesByExtension.Add("jpe", "image/jpeg");
            ContentTypesByExtension.Add("mp3", "audio/mpeg");
            ContentTypesByExtension.Add("swf", "application/x-shockwave-flash");
            ContentTypesByExtension.Add("zip", "application/zip");
            ContentTypesByExtension.Add("pdf", "application/pdf");
        }

        public BinaryResource FindById(Guid id)
        {
            return context.BinaryResources.Where(br => br.ID == id).FirstOrDefault();
        }

        public void Delete(Guid id)
        {
            var deleteMe = context.BinaryResources.SingleOrDefault(br => br.ID == id);
            if (deleteMe != null)
            {
                deleteMe.Binary = new byte[1];
                context.BinaryResources.DeleteOnSubmit(deleteMe);
                context.SubmitChanges();
            }
        }

        public IEnumerable<BinaryResource> GetAll()
        {
            return context.BinaryResources.Select(br => br);
        }

        public string GetContentType(string fileName)
        {
            string returnValue = null;
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                string key = GetExtension(fileName);
                if (ValidContentType(key))
                {
                    returnValue = ContentTypesByExtension[key];
                }
            }
            return returnValue;
        }

        public bool ValidContentType(string fileName)
        {
            return ContentTypesByExtension.ContainsKey(GetExtension(fileName));
        }

        private string GetExtension(string fileName)
        {
            string extension = fileName;
            int startIndex = 0;
            if (fileName.Contains("."))
            {
                startIndex = fileName.LastIndexOf(".") + 1;
            }
            if (startIndex != 0 && startIndex < fileName.Length)
            {
                extension = fileName.Substring(startIndex);
            }
            return extension;
        }

        public void Create(BinaryResource br)
        {
            context.BinaryResources.InsertOnSubmit(br);
        }
        public void Save()
        {
            context.SubmitChanges();
        }
    }
}
